package info.openlvb.openlvg.client.handler;

import info.openlvb.openlvg.client.Openlvg;
import info.openlvb.openlvg.client.base.Account;
import info.openlvb.openlvg.client.base.home.HomePanel;
import info.openlvb.openlvg.client.images.OpenPageImages;
import info.openlvb.openlvg.client.service.LdapService;
import info.openlvb.openlvg.client.service.LdapServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.gwtext.client.widgets.MessageBox;


public class Login_Ok_Handler implements ClickHandler, KeyUpHandler{

	private Openlvg openPage;
	
	private final LdapServiceAsync ldapService = GWT
	.create(LdapService.class);

	OpenPageImages images = GWT.create(OpenPageImages.class);

	public Login_Ok_Handler(Openlvg openPage){ //RootPanel rootPanel, String login, String password){
		this.openPage = openPage;
	}
	public void onClick(ClickEvent event) {
		authentification();	
	}
	public void onKeyUp(KeyUpEvent event) {
		if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
			authentification();
		}
	}

	private void authentification() {
//		ldapService.login(openPage.getLogin(), openPage.getPassword(), 
//				new AsyncCallback<Account>() {
//
//					@Override
//					public void onFailure(Throwable caught) {
//						final DecoratedPopupPanel simplePopup = new DecoratedPopupPanel(true);
//						simplePopup.setWidget(new HTML("probleme d'authentification"));
//						simplePopup.center();
//						simplePopup.show();
//					}
//
//					
//					public void onSuccess(Account result) {
//						if(result==null){
//							MessageBox.alert("L'authentification a échouée");
//						}else{
							openPage.getRootPanel().clear();
							FlowPanel hp = new FlowPanel();
							hp.setSize("100%","auto");
							hp.add(images.openlvg().createImage());
							hp.add(new HTML("<div id=\"connected\">Connected : "+openPage.getLogin()+" | <a href=\"\">log out</a></div><hr>"));
							ScrollPanel vp = new ScrollPanel();
							vp.setSize("100%","auto");
							vp.setStyleName("VP");
							vp.add(new HomePanel());
							openPage.getRootPanel().add(hp);
							openPage.getRootPanel().add(vp);
//						}
//					}});
	}
}
